package org.shangma.service.impl;

import io.seata.core.context.RootContext;
import org.shangma.common.Account;
import org.shangma.mapper.AccountMapper;
import org.shangma.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public Account findAccountById(int accountId) {
        return accountMapper.findAccountById(accountId);
    }

    @Override
    public void reduceMoney(double money, int accountId) {
        System.out.println("是否在全局事务中：" + RootContext.inGlobalTransaction());
        System.out.println("事务id: " + RootContext.getXID());
        System.out.println("事务类型: " + RootContext.getBranchType());
        Account account = accountMapper.findAccountById(accountId);
        if(account.getMoney()<money){
            throw new RuntimeException("资金不足");
        }
        accountMapper.reduceMondy(money,accountId);
    }
}
